package com.oig.sys.workflow.app.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.msg.constants.MsgEnums;
import com.oig.sys.workflow.app.IAppAutoRuleService;
import com.oig.sys.workflow.dto.AutoOperationDto;
import com.oig.sys.workflow.dto.AutoRuleDto;
import com.oig.sys.workflow.service.ITbAutoRuleService;
import com.oig.sys.workflow.vo.AutoRuleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RequiredArgsConstructor
public class AppAutoRuleServiceImpl implements IAppAutoRuleService {

    private final ITbAutoRuleService autoRuleService;



    @Override
    public AutoRuleVo addAutoRule(AutoRuleDto autoRuleDto) {
        autoRuleDto.getAutoOperationDtoList().forEach(this::checkCfg);
        return this.autoRuleService.addAutoRule(autoRuleDto);
    }


    private void checkCfg(AutoOperationDto autoOperationDto){
        if (CommonEnum.FlagEnum.FALSE_FLAG.getCode().equals(autoOperationDto.getNotifyFlag())
                &&CommonEnum.FlagEnum.FALSE_FLAG.getCode().equals(autoOperationDto.getTaskFlag())){
            throw new BadRequestException("无执行操作");
        }
        if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(autoOperationDto.getNotifyFlag())){
            if (StringUtils.isBlank(autoOperationDto.getNotifyTitle())){
                throw new BadRequestException("无通知标题");
            }
            if (StringUtils.isBlank(autoOperationDto.getNotifyContent())){
                throw new BadRequestException("无通知内容");
            }
            if (autoOperationDto.getPushChannelId()==null){
                throw new BadRequestException("渠道id必填");
            }
            if (StringUtils.isBlank(autoOperationDto.getPushChannelName())){
                throw new BadRequestException("渠道名称必填");
            }
            if (MsgEnums.MsgType.TARGET.getCode().equals(autoOperationDto.getNotifyType())){	//定向通知
                if (StringUtils.isBlank(autoOperationDto.getReceiverType())){
                    throw new BadRequestException("定向通知需指定接收方");
                }
                if (!MsgEnums.ReceiverType.ALL.getCode().equals(autoOperationDto.getReceiverType())){
                    if (autoOperationDto.getReceiverId()==null||StringUtils.isBlank(autoOperationDto.getReceiverName())){
                        throw new BadRequestException("指定了接收方类型，必须指定接收方");
                    }
                }
            }
        }
        if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(autoOperationDto.getTaskFlag())){
            if (StringUtils.isBlank(autoOperationDto.getTaskTitle())){
                throw new BadRequestException("无任务标题");
            }
            if (StringUtils.isBlank(autoOperationDto.getTaskContent())){
                throw new BadRequestException("无任务内容");
            }
            if (autoOperationDto.getTaskUserId()==null){
                throw new BadRequestException("无任务处理人");
            }
            if (autoOperationDto.getTaskWatchId()==null){
                throw new BadRequestException("无任务跟进人");
            }
        }
    }


    @Override
    public AutoRuleVo updateAutoRule(AutoRuleDto autoRuleDto) {
        autoRuleDto.getAutoOperationDtoList().forEach(this::checkCfg);
        return this.autoRuleService.updateAutoRule(autoRuleDto);
    }

    @Override
    public AutoRuleVo getAutoRule(Integer id) {
        return this.autoRuleService.getAutoRule(id);
    }

    @Override
    public void cancelAutoRule(Integer id) {
        this.autoRuleService.cancelAutoRule(id);
    }

    @Override
    public void delAutoRule(Integer id) {
        this.autoRuleService.delAutoRule(id);
    }

    @Override
    public void disableAutoRule(Integer id) {
        this.autoRuleService.disableAutoRule(id);
    }

    @Override
    public void enableAutoRule(Integer id) {
        this.autoRuleService.enableAutoRule(id);
    }
}
